<template>
  <div class='container'>
    <!-- 表单 -->
    <el-dialog title="新增技巧" :visible="dialogVisible" width="30%" @close='handleClose' @closed='onAddClose'>
      <el-form label-width="80px" :model="pubForm" :rules="rules" ref="oh">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="pubForm.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" class="ql-editor" prop="articleBody">
          <!-- 使用 v-model 进行双向的数据绑定 -->
          <quill-editor v-model="pubForm.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input v-model="pubForm.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <!-- footer插槽 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click='handleClose'>取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
import { add, update } from '../../api/hmmm/articles'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    pubForm: {
      type: Object,
      required: true
    }
  },
  data () {
    return {

      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: ['blur', 'change'] }
        ],
        articleBody: [
          { required: true }
        ]
      }
    }
  },

  methods: {
    onAddClose () {
      this.$refs.oh.resetFields()
    },
    // 新增
    async handleSubmit () {
      if (!this.pubForm.id) {
        const ret = await add(this.pubForm)
        if (ret.status === 200) {
          this.$emit('toggle-box', false)
          this.$parent.loadDepartList()
          this.$emit('reset', this.pubForm)
        }
      } else if (this.pubForm.id) {
        const ret2 = await update(this.pubForm)
        console.log(ret2)
        this.$emit('toggle-box', false)
        this.$parent.loadDepartList()
        this.$emit('reset', this.pubForm)
      }
    },
    // 关闭弹窗
    handleClose () {
      this.$emit('update:dialog-visible', false)
    }
  }

}

</script>

<style scoped lang='less'>
/deep/.ql-editor {
  min-height: 200px;
}
</style>
